
<template>
  <div class="com-container">
    <div class="com-chart" ref="rank"></div>
  </div>
</template>

<script>
import "../assets/js/essos.js";
export default {
  data() {
    return {
      allData: [],
      myCharts: null,
      startValue: 0,
      timerId: null,
    };
  },
  methods: {
    //初始化报表
    async initCharts() {
      this.myCharts = this.$echarts.init(this.$refs.rank, "essos");
      //初始化报表配置项，与数据无关
      const initOptions = {
        tooltip: { show: true },
        grid: {
          top: "40%",
          left: "5%",
          bottom: "5%",
          right: "5%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          
        },
        yAxis: {
          type: "value",
           name: "年销售金额（万）",
        },
        series: [
          {
            type: "bar",
          },
        ],
      };
      this.myCharts.setOption(initOptions);
      this.createReports();
      //鼠标移入动画停止
      this.myCharts.on('mouseover',()=>{
        clearInterval(this.timerId)
      })    
    //鼠标移出动画开始
    this.myCharts.on('mouseout',()=>{
        this.startInterval()
      }) 
    },
    //创建报表
    async createReports() {
      const { data } = await this.$axios.get("http://localhost:3000/trender");
      this.allData = data.map.data;
      var yData = [];
      for (let i = 0; i < this.allData.length; i++) {
        let obj = {};
        let item = this.allData[i].data;
        let city = this.allData[i].name;
        let sum = item.reduce((total, currentValue) => {
          let num = Number(total);
          let curValue = Number(currentValue);
          return num + curValue;
        });
        obj.value = sum;
        obj.name = city;
        yData.push(obj);
      }

      const seriesData = yData.sort((a, b) => {
        return b.value - a.value;
      });

      //获取x轴数据
      const xData = seriesData.map((item, index) => {
        return item.name;
      });
      //获取地区总数据
      const itemData = seriesData.map((item, index) => {
        return item.value;
      });
      //根据数据等级颜色变化
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];

      //报表数据样式等设计
      var dataOptions = {
        title: {
          text: "▎ 地区销售排行",
          left: 20,
          top: 20,
          textStyle: {
            color: "#893448",
            // fontSize: 25,
          },
        },
        xAxis: {
          data: xData,
        },
        series: [
          {
            // barWidth: 80,
            data: itemData,
            itemStyle: {
              // barBorderRadius: [100, 100, 0, 0],
              //颜色渐变
              color: (arg) => {
                let targetColorArr = colorArr[0];
                if (arg.value >= 2300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value >= 2000) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  { offset: 0, color: targetColorArr[0] },
                  { offset: 1, color: targetColorArr[1] },
                ]);
              },
            },
          },
        ],
        dataZoom: {
          type: "inside",
          startValue: this.startValue,
        },
      };
      this.myCharts.setOption(dataOptions);
      this.startInterval();
    },

    //定时平移动画
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;

        if (this.startValue > this.allData.length - 1) {
          this.startValue = 0;
        }
        this.createReports();
      }, 2000);
    },
    // 当浏览器的大小发生变化的时候, 会调用的方法, 来完成屏幕的适配
    screenAdapter() {
      const titleFontSize = (this.$refs.rank.offsetWidth / 100) * 3.6;
      this.titleFontSize = titleFontSize;
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize/1.5,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0.5 * titleFontSize, 0.5 * titleFontSize, 0, 0],
            },
          },
        ],
      };
      this.myCharts.setOption(adapterOption);
      this.myCharts.resize();
    },
    
  },
  mounted: function () {
    this.initCharts();
    this.createReports();
    this.screenAdapter();
     window.addEventListener("resize", this.screenAdapter);
    
  },
};
</script>

<style>
.com-container{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.com-chart{
  width: 100%;
  height: 100%;
  overflow: hidden;
}
</style>